% Automatic Selection window configuration
% June 2016
% Hamzeh Sadeghisorkhani

%% Automatic Selection panel
h.panel(3)= uipanel(mainfig,'visible','off','Position',[.235 .15 .74 .8]);
set(h.panel(3),'BackgroundColor',panel_color)



%% Text boxes
%-------------------------------- 
uicontrol(h.panel(3),'Style','text', 'String','Automatic Selection',...
    'HorizontalAlignment','left','Fontweight','bold',...
    'Units','normalized','Position',[.05 .92 .3 .05],...
    'BackgroundColor',panel_color);

uicontrol(h.panel(3),'Style','text', 'String','Basic Criteria',...
    'HorizontalAlignment','left','Fontweight','bold',...
    'Units','normalized','Position',[.01 .83 .4 .05],...
    'BackgroundColor',panel_color);

uicontrol(h.panel(3),'Style','text', 'String','Max deviation',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.1 .76 .2 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','Cycle',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.43 .76 .2 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','Energy',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.7 .76 .2 .05],...
    'BackgroundColor',panel_color);


%-------------------------------- 
uicontrol(h.panel(3),'Style','text', 'String','Smoothness Criteria',...
    'HorizontalAlignment','left','Fontweight','bold',...
    'Units','normalized','Position',[.01 .64 .4 .05],...
    'BackgroundColor',panel_color);


uicontrol(h.panel(3),'Style','text', 'String','Slope',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.05 .56 .2 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','abs slope upward',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.3 .56 .2 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','abs slope downward',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.64 .56 .25 .05],...
    'BackgroundColor',panel_color);


%-------------------------------- 
uicontrol(h.panel(3),'Style','text', 'String','Using Anchors',...
    'HorizontalAlignment','left','Fontweight','bold',...
    'Units','normalized','Position',[.01 .44 .4 .05],...
    'BackgroundColor',panel_color);



uicontrol(h.panel(3),'Style','text', 'String','Pr1',...
    'HorizontalAlignment','center',...
    'Units','normalized','Position',[.42 .4 .1 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','Pr2',...
    'HorizontalAlignment','center',...
    'Units','normalized','Position',[.54 .4 .1 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','< T <',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.5 .4 .06 .05],...
    'BackgroundColor',panel_color);


uicontrol(h.panel(3),'Style','text', 'String','Slope',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.05 .32 .2 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','SlopePr2-Pr1',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.25 .32 .2 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','SlopePr2',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.5 .32 .2 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','SlopePr1',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.75 .32 .25 .05],...
    'BackgroundColor',panel_color);


uicontrol(h.panel(3),'Style','text', 'String','T < Pr1',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.5 .22 .15 .05],...
    'BackgroundColor',panel_color);
uicontrol(h.panel(3),'Style','text', 'String','Slope',...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.05 .18 .2 .05],...
    'BackgroundColor',panel_color);

%% Editable text 
%-------------------------------- 
h2.automatic= uicontrol(h.panel(3),'Style','checkbox', 'String','',...
    'HorizontalAlignment','left','Value',1,...
    'Tag','automatic',...
    'Units','normalized','Position',[.3 .935 .1 .04],...
    'BackgroundColor',panel_color,'callback',@checkbox_cbk);


h2.maxdev= uicontrol(h.panel(3),'Style','edit', 'String',params.maxdev,...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.27 .775 .1 .04],...
    'BackgroundColor','w');
h2.cycle= uicontrol(h.panel(3),'Style','edit', 'String',params.cycle,...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.51 .775 .1 .04],...
    'BackgroundColor','w');
h2.energy= uicontrol(h.panel(3),'Style','edit', 'String',params.energy,...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.79 .775 .1 .04],...
    'BackgroundColor','w');


%-------------------------------- 
h2.slope(1)= uicontrol(h.panel(3),'Style','edit', 'String',params.slope{1},...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.13 .575 .1 .04],...
    'BackgroundColor','w');
h2.slope(2)= uicontrol(h.panel(3),'Style','edit', 'String',params.slope{2},...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.51 .575 .1 .04],...
    'BackgroundColor','w');
h2.slope(3)= uicontrol(h.panel(3),'Style','edit', 'String',params.slope{3},...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.88 .575 .1 .04],...
    'BackgroundColor','w');


%--------------------------------
h2.anchor= uicontrol(h.panel(3),'Style','checkbox', 'String','',...
    'HorizontalAlignment','left','Value',1,...
    'Tag','anchor',...
    'Units','normalized','Position',[.2 .455 .1 .04],...
    'BackgroundColor',panel_color,'callback',@checkbox_cbk);


h2.pr1= uicontrol(h.panel(3),'Style','edit', 'String',params.pr1,...
    'HorizontalAlignment','right',...
    'Units','normalized','Position',[.34 .415 .1 .04],...
    'BackgroundColor','w');
h2.pr2= uicontrol(h.panel(3),'Style','edit', 'String',params.pr2,...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.62 .415 .1 .04],...
    'BackgroundColor','w');


h2.slope(4)= uicontrol(h.panel(3),'Style','edit', 'String',params.slope{4},...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.13 .335 .1 .04],...
    'BackgroundColor','w');
h2.slope(5)= uicontrol(h.panel(3),'Style','edit', 'String',params.slope{5},...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.39 .335 .1 .04],...
    'BackgroundColor','w');
h2.slope(6)= uicontrol(h.panel(3),'Style','edit', 'String',params.slope{6},...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.61 .335 .1 .04],...
    'BackgroundColor','w');
h2.slope(7)= uicontrol(h.panel(3),'Style','edit', 'String',params.slope{7},...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.85 .335 .1 .04],...
    'BackgroundColor','w');


h2.slope(8)= uicontrol(h.panel(3),'Style','edit', 'String',params.slope{8},...
    'HorizontalAlignment','left',...
    'Units','normalized','Position',[.13 .195 .1 .04],...
    'BackgroundColor','w');



%% Saving buttons

uicontrol(h.panel(3),'style','pushbutton', 'string','Default',...
    'ToolTipString','Load default setting','Tag','default3',...
    'units','normalized','position',[.1 .03 .18 .07], ...
    'callback',@default_cbk);

uicontrol(h.panel(3),'style','pushbutton', 'string','Latest',...
    'ToolTipString','Load latest setting','Tag','latest3',...
    'units','normalized','position',[.41 .03 .18 .07], ...
    'callback',@latest_cbk);

uicontrol(h.panel(3),'style','pushbutton', 'string','Save',...
    'ToolTipString','Save setting changes','Tag','save3',...
    'units','normalized','position',[.72 .03 .18 .07], ...
    'callback',@save_cbk);



%% Setting values

if strcmp(params.automatic,'yes')
    set (h2.automatic,'value',1);   
    set(h2.maxdev,'Enable','on');
    set(h2.cycle,'Enable','on');
    set(h2.energy,'Enable','on');
    set(h2.slope(1:3),'Enable','on');
    set(h2.anchor,'Enable','on');
    if get(h2.anchor,'Value') 
        set(h2.slope(4:8),'Enable','on');           
        set(h2.pr1,'Enable','on');
        set(h2.pr2,'Enable','on');
    end
elseif strcmp(params.automatic,'no')
    set (h2.automatic,'value',0);  
    set(h2.maxdev,'Enable','off');
    set(h2.cycle,'Enable','off');
    set(h2.energy,'Enable','off');
    set(h2.slope(:),'Enable','off');
    set(h2.anchor,'Enable','off');
    set(h2.pr1,'Enable','off');
    set(h2.pr2,'Enable','off');
end

if strcmp(params.anchor,'yes')
    if get(h2.automatic,'Value')
        set (h2.anchor,'value',1);  
        set(h2.slope(4:8),'Enable','on');
        set(h2.pr1,'Enable','on');
        set(h2.pr2,'Enable','on');
    end
elseif strcmp(params.anchor,'no')
    set (h2.anchor,'value',0);  
    set(h2.slope(4:8),'Enable','off');
    set(h2.pr1,'Enable','off');
    set(h2.pr2,'Enable','off');
end
